package com.wangsd.web.coupon.service.impl;

import com.wangsd.common.utils.LuckyMachineUtils;
import com.wangsd.web.coupon.entity.BigWheelDetail;
import com.wangsd.web.coupon.mapper.BigWheelDetailMapper;
import com.wangsd.web.coupon.mapper.BigWheelMapper;
import com.wangsd.web.coupon.service.BigWheelDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-22
 */
@Service
public class BigWheelDetailServiceImpl extends ServiceImpl<BigWheelDetailMapper, BigWheelDetail> implements BigWheelDetailService {

    @Resource
    BigWheelDetailMapper bigWheelDetailMapper;

    @Override
    public List<BigWheelDetail> selectBigWheelList(Long id) {
        BigWheelDetail bigWheel = new BigWheelDetail();
        bigWheel.setCompanyId(id);
        bigWheel.setStatus(1);
        List<BigWheelDetail> bigWheels = bigWheelDetailMapper.selectListXml(bigWheel);
        return bigWheels;
    }

    @Override
    public BigWheelDetail runBigWheel(Long id, String openId) {
        List<BigWheelDetail> bigWheels = this.selectBigWheelList(id);
        List<String> ids = bigWheels.stream().map(bigWheel -> bigWheel.getObjectId()).collect(Collectors.toList());
        List<Long> rates = bigWheels.stream().map(bigWheel -> bigWheel.getRate()).collect(Collectors.toList());
        List<Double> collect = rates.stream().map(rate -> rate * 0.01).collect(Collectors.toList());
        String run = LuckyMachineUtils.run(ids, collect);
        /*返回当前商品角度*/
        int angle = 0;
        /*平均每个商品的角度*/
        int angleN = 360/ids.size();
        for (int i = 0; i < ids.size(); i++) {
            if(ids.get(i) == run){
                angle = i*angleN;
            }
        }
        BigWheelDetail bigWheelDetail = new BigWheelDetail();
        bigWheelDetail.setObjectId(run);
        for (BigWheelDetail detail:bigWheels) {
            if(run.equals(detail.getObjectId())){
                bigWheelDetail.setObjectType(detail.getObjectType());
                bigWheelDetail.setName(detail.getName());
            }
        }
        bigWheelDetail.setAngle(angle);
        return bigWheelDetail;
    }
}
